
function towSum(nums, target) {

  let result = []

  for (let i = 0; i < nums.length; i++) {

    for (let j = i + 1; j < nums.length; j++) {

      const r = nums[i] + nums[j]

      if (r === target) {
        result = [i, j]
      }

    }

  }

  return result

}

// 只用一个循环
// x + y = t
// t - y = x

function twoSum2(nums, target) {

  let x = null;
  let y = null;
  let nums2 = JSON.parse(JSON.stringify(nums))
  let result = []

  for (let i = 0; i < nums.length; i++) {
    x = nums[i]
    y = target - x
    nums2.splice(i, 1, null)
    let t = nums2.indexOf(y)

    if (t !== -1) {
      result = [i, t]
      break;
    }

  }

  return result

}


function twoSum3(nums, target) {
  const map = new Map();

  for (let i = 0; i < nums.length; i++) {
    let r = target - nums[i]
    let t = map.has(r)
    if (t) {
      return [map.get(r), i]
    } else {
      map.set(nums[i], i)
    }

  }
  return []
}

console.log(twoSum3([3, 2, 3], 6));